<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        user-select: none;
      }
      ul {
        list-style: none;
      }

      #wrap {
        width: 300px;
        padding: 15px 0;
        border: 1px solid black;
        border-radius: 20px;
        margin: 100px auto 0;
        background-color: rgba(0, 0, 0, 0.5);
      }

      #wrap ul li {
        line-height: 50px;
        text-indent: 40px;
        cursor: pointer;
        background: url(https://s1.ax1x.com/2020/09/26/0iBW26.jpg) no-repeat
          20px center;
      }

      #wrap ul li.checked {
        background: url(https://s1.ax1x.com/2020/09/26/0iBIqe.jpg) no-repeat
          20px center;
      }

      #wrap ul li:nth-child(4n + 1) {
        background-color: #2ecc71;
      }
      #wrap ul li:nth-child(4n + 2) {
        background-color: #e67e22;
      }
      #wrap ul li:nth-child(4n + 3) {
        background-color: #f1c40f;
      }
      #wrap ul li:nth-child(4n + 4) {
        background-color: #9b59b6;
      }

      #wrap ul li span {
        color: #fff;
        font-size: 16px;
        margin-left: 40px;
        text-shadow: 0 0 10px rgba(0, 0, 0, 0.8);
      }

      #wrap .btn {
        display: flex;
        justify-content: space-evenly;
        height: 60px;
        line-height: 60px;
      }
      #wrap .btn div:not(.reverse) {
        width: 35%;
        cursor: pointer;
        display: flex;
        justify-content: center;

        background: url(https://s1.ax1x.com/2020/09/26/0iBW26.jpg) no-repeat
          right center;
      }

      #wrap .btn div.checked {
        background: url(https://s1.ax1x.com/2020/09/26/0iBIqe.jpg) no-repeat
          right center;
      }
    </style>
  </head>
  <body>
    <div id="wrap">
      <ul>
        <li><span>刘一</span></li>
        <li><span>陈二</span></li>
        <li><span>张三</span></li>
        <li><span>李四</span></li>
        <li><span>王五</span></li>
        <li><span>赵六</span></li>
        <li><span>孙七</span></li>
        <li><span>周八</span></li>
      </ul>

      <div class="btn">
        <div class="all">全选</div>
        <div class="noAll checked">全不选</div>
        <div class="reverse">反选</div>
      </div>
    </div>
    <script>
      let aLi = document.querySelectorAll("#wrap ul li");
      let oAll = document.querySelector("#wrap .btn .all");
      let oNoAll = document.querySelector("#wrap .btn .noAll");
      let oReverse = document.querySelector("#wrap .btn .reverse");

      //默认0什么都没有选中
      let count = 0;

      //循环为li绑定点击事件
      for (let i = 0; i < aLi.length; i++) {
        aLi[i].mark = false; //false代表没有选中

        aLi[i].onclick = function () {
          if (aLi[i].mark) {
            count--;
            aLi[i].mark = false;
          } else {
            // 点击了代表选中 使得count + 1
            count++;
            aLi[i].mark = true;
          }

          console.log(count);
          //偷狗 来添加类名
          aLi[i].classList.toggle("checked");

          switch (count) {
            case 8:
              oAll.classList.add("checked");
              oNoAll.classList.remove("checked");
              break;
            case 0:
              oAll.classList.remove("checked");
              oNoAll.classList.add("checked");
              break;
            default:
              oAll.classList.remove("checked");
              oNoAll.classList.remove("checked");
              break;
          }
        };
      }

      //全选功能
      oAll.onclick = function () {
        for (let i = 0; i < aLi.length; i++) {
          aLi[i].classList.add("checked");

          aLi[i].mark = true;
        }

        count = 8;

        //switch (count) {
        //  case 8:
        //    oAll.classList.add("checked");
        //    oNoAll.classList.remove("checked");
        //    break;
        //  case 0:
        //    oAll.classList.remove("checked");
        //    oNoAll.classList.add("checked");
        //    break;
        //  default:
        //    oAll.classList.remove("checked");
        //    oNoAll.classList.remove("checked");
        //    break;
        //}

        countNum(count);
      };

      //全不选功能
      oNoAll.onclick = function () {
        for (let i = 0; i < aLi.length; i++) {
          aLi[i].classList.remove("checked");

          aLi[i].mark = false;
        }

        count = 0;
        //switch (count) {
        //  case 8:
        //    oAll.classList.add("checked");
        //    oNoAll.classList.remove("checked");
        //    break;
        //  case 0:
        //    oAll.classList.remove("checked");
        //    oNoAll.classList.add("checked");
        //    break;
        //  default:
        //    oAll.classList.remove("checked");
        //    oNoAll.classList.remove("checked");
        //    break;
        //}

        countNum(count);
      };

      //反选功能
      oReverse.onclick = function () {
        for (let i = 0; i < aLi.length; i++) {
          aLi[i].classList.toggle("checked");

          if (aLi[i].mark) {
            count--;
            aLi[i].mark = false;
          } else {
            // 点击了代表选中 使得count + 1
            count++;
            aLi[i].mark = true;
          }

          //  aLi[i].mark = !aLi[i].mark;
        }

        //switch (count) {
        //  case 8:
        //    oAll.classList.add("checked");
        //    oNoAll.classList.remove("checked");
        //    break;
        //  case 0:
        //    oAll.classList.remove("checked");
        //    oNoAll.classList.add("checked");
        //    break;
        //  default:
        //    oAll.classList.remove("checked");
        //    oNoAll.classList.remove("checked");
        //    break;
        //}

        countNum(count);
      };

      function countNum(n) {
        switch (n) {
          case 8:
            oAll.classList.add("checked");
            oNoAll.classList.remove("checked");
            break;
          case 0:
            oAll.classList.remove("checked");
            oNoAll.classList.add("checked");
            break;
          default:
            oAll.classList.remove("checked");
            oNoAll.classList.remove("checked");
            break;
        }
      }
    </script>
  </body>
</html>
